package freespiritsocial.io.user.ui.fragment.message

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import com.scwang.smart.refresh.layout.util.SmartUtil
import freespiritsocial.io.user.R
import freespiritsocial.io.user.databinding.DialogReportUserBinding
import freespiritsocial.io.user.ext.hideSoftKeyboard
import freespiritsocial.io.user.viewmodel.ChatViewModel
import freespiritsocial.io.user.weight.MyPickupViewBuilder
import me.hgj.jetpackmvvm.base.fragment.BaseBottomSheetDialogFragment

class ReportUserDialogFragment: BaseBottomSheetDialogFragment() {

    companion object {
        fun crate(mViewModel: ChatViewModel) = ReportUserDialogFragment().apply {
            this.mViewModel = mViewModel
        }
    }

    private val topicList = arrayListOf<String>()

    private val pickupView by lazy(LazyThreadSafetyMode.NONE) {
        MyPickupViewBuilder(
            requireContext(),
            dialog?.window?.decorView
        ) { i: Int,_: Int,_: Int,_: View? ->
            mViewModel.topic.set(topicList[i])
        }.build<String>().apply {
            setPicker(topicList)
        }
    }

    private lateinit var mViewModel: ChatViewModel

    private lateinit var mDatabind: DialogReportUserBinding
    override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
        mDatabind = DataBindingUtil.inflate(inflater,R.layout.dialog_report_user,container,false)
        mDatabind.lifecycleOwner = this
        return mDatabind.root
    }

    override fun onViewCreated(view: View,savedInstanceState: Bundle?) {
        topOffset = SmartUtil.dp2px(50f)
        mDatabind.model = mViewModel
        mDatabind.tvSelectIssue.setOnClickListener {
            hideSoftKeyboard(mDatabind.tvMessage)
            if (topicList.isEmpty())
                mViewModel.chatReportTopic {
                    topicList.addAll(it)
                    pickupView.show()
                }
            else
                pickupView.show()
        }
        mDatabind.tvCancel.setOnClickListener {
            hideSoftKeyboard(mDatabind.tvMessage)
            dismissAllowingStateLoss()
        }
        mDatabind.tvSubmit.setOnClickListener {
            hideSoftKeyboard(mDatabind.tvMessage)
            mViewModel.chatReport {
                dismissAllowingStateLoss()
            }
        }
    }

}